home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-20
/
morse2.zip
/
MORSE.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1991-02-27
|
10KB
|
360 lines
Program Morse;
{
02/26/90
Morse Code Program. Submitted to public domain by
Jerry Adkins KB8YA/6 COMPUSERVE ID: 70455,112.
7770 Regents Rd. #226
San Diego, CA 92122
This program makes use of direct array indexing, instead of using a
lookup table. This makes very high translation speeds possible.
You may experiment with the typed constants Hz and Pause for different
default tone and speed.
The files CQ.TXT and QSO.TXT reflect a typical conversation you are
likely to hear on the novice bands.
It was done as a one evening experiment. Enjoy.
}
Uses
Crt;
Type
String6 = String[6];
Const
Esc = #27;
Hz : Integer = 800;
Pause : Integer = 200;
DONE = FALSE;
{ The array MorseChar has indices corresponding to the ordinal
value of the chars normally used in the Morse alphabet. This
allows high speed translation without using lookup tables. }
MorseChar : Array[44..90] Of String6 =
('--..--' { , }, '-...-' { - },
'.-.-.-' { . }, '-..-.' { / },
'-----' { 0 }, '.----' { 1 },
'..---' { 2 }, '...--' { 3 },
'....-' { 4 }, '.....' { 5 },
'-....' { 6 }, '--...' { 7 },
'---..' { 8 }, '----.' { 9 },
'','','','','',
'..--..' { ? } ,'',
'.-' { A }, '-...' { B },
'-.-.' { C }, '-..' { D },
'.' { E }, '..-.' { F },
'--.' { G }, '....' { H },
'..' { I }, '.---' { J },
'-.-' { K }, '.-..' { L },
'--' { M }, '-.' { N },
'---' { O }, '.--.' { P },
'--.-' { Q }, '.-.' { R },
'...' { S }, '-' { T },
'..-' { U }, '...-' { V },
'.--' { W }, '-..-' { X },
'-.--' { Y }, '--..' { Z });
LegalChars : Set Of Char = [#44..#57,#63,#65..#90];
Var
Cmd : Integer;
Procedure SoundOut(n:Byte);
{ ***********************************************************
Store MorseChar[n] in Element string. Scan through string
and sound speaker based on a dot or dash. Use set Frequency
in Hz and delay time in Pause.
***********************************************************}
Var
i : Byte;
Element : String6;
Begin
Element := MorseChar[n];
For i := 1 to Length(Element) Do
Begin
Case Element[i] Of
'-' : Begin
Sound(Hz);
Delay(Pause);
End;
'.' : Begin
Sound(Hz);
Delay(Pause Div 2);
End;
End; { Case }
NoSound;
Delay(Pause Div 3);
End; { i }
Delay(Pause);
End; { SoundOut }
Procedure OutPutChar(Ch:Char);
{ ***********************************************************
Convert Ch to number and send result to Procedure SoundOut
***********************************************************}
Var
n : Byte;
Begin
If Ch <> Esc Then
Write(Ch);
If Ch = #32 Then
Delay(Pause); { Delay for space }
n := Ord(Ch);
If Not (Chr(n) In LegalChars) Then Exit; { Not morse char }
SoundOut(n);
End; { OutPutChar }
Procedure MorseKey;
{ ***********************************************************
Input from keyboard and process keystrokes. Exit on Esc.
***********************************************************}
Var
Ch : Char;
Begin
ClrScr;
WriteLn('Start typing. Press ESC to exit.');
Repeat
Ch := ReadKey;
If Ch = #13 Then Writeln;
Ch := UpCase(Ch);
OutPutChar(Ch);
Until Ch = Esc;
End; { MorseKey }
Procedure OutPutLine(Line:String; Var Abort:Boolean);
{ ***********************************************************
Read text line from a file and process each char for output.
***********************************************************}
Var
i : Byte;
Ch : Char;
Begin
For i := 1 to Length(Line) Do
Begin
If KeyPressed Then
Begin
Ch := ReadKey;
If Ch = Esc Then
Begin
Abort := True;
Exit;
End;
End;
Ch := UpCase(Line[i]);
OutPutChar(Ch);
End;
WriteLn;
End; { OutPutLine }
Procedure MorseRead;
{ ***********************************************************
Open text file and send to speaker as morse code.
***********************************************************}
Var
TextFile : Text;
Line, FileName : String;
Err : Integer;
Ch : Char;
Abort : Boolean;
Begin
Abort := False;
FileName := '';
ClrScr;
GoToXY(20,12);
Write('Filename: ');
ReadLn(FileName);
If FileName = '' Then Exit;
{$I-}
Assign(TextFile,FileName);
Reset(TextFile);
Err := IoResult;
{$I+}
If Err <> 0 Then
Begin
GoToXY(20,13);
Write(#7,FileName,' not found. Press ENTER. ');
ReadLn;
Exit;
End;
ClrScr;
WriteLn('Reading ',FileName,'. Press Esc to abort.');
While Not Eof(TextFile) Do
Begin
ReadLn(TextFile,Line);
OutputLine(Line,Abort);
If Abort Then
Begin
Close(TextFile);
Exit;
End;
End;
Close(TextFile);
End; { MorseRead }
Procedure RandomCode;
{ ***********************************************************
Random code practice in sets of four chars.
***********************************************************}
Var
n, c : Byte;
Ch : Char;
Begin
n := 0;
Ch := #0;
Randomize;
ClrScr;
WriteLn('Random code practice. Press ESC to abort.');
Repeat
If KeyPressed Then Ch := ReadKey;
Repeat
c := Random(90);
Until Chr(c) In LegalChars; { Acceptable Morse Chars }
Write(Chr(c));
SoundOut(c);
Inc(n);
If n > 3 Then
Begin
Write(#32);
Delay(Pause);
n := 0;
End;
Until Ch = Esc;
End; { RandomCode }
Procedure CodeTest;
{ ***********************************************************
Random code sent. User types char in response to sound.
***********************************************************}
Var
Ch : Char;
n : Byte;
Ok : Boolean;
CharsTyped, CharsMissed, Score : Real;
Begin
CharsTyped := 0;
CharsMissed := 0;
Randomize;
ClrScr;
WriteLn('Type your response after hearing the sound. Press ESC to exit.');
Repeat
Repeat
n := Random(90);
Until (n > 43) And (Chr(n) In LegalChars);
SoundOut(n);
Ch := UpCase(ReadKey);
Ok := Ch In LegalChars;
If Ok Then
CharsTyped := CharsTyped + 1;
If Ord(Ch) = n Then
TextColor(White) Else
If Ok Then
CharsMissed := CharsMissed + 1;
If Ok Then
Write(Ch);
If Ord(Ch) <> n Then
Begin
TextColor(Red);
Write(' ',Chr(n));
End;
WriteLn;
Until Ch = Esc;
Score := (CharsMissed/CharsTyped) * 100;
TextColor(White);
ClrScr;
GoToXY(1,12);
If (CharsTyped > 1) And (CharsMissed < 1) Then
Write('Congratulations!!! Your score is 100%') Else
Begin
Write('You missed ',CharsMissed:3:0,' Out of ');
Write(CharsTyped:4:0);
Write('. Your score is ',100-Score:3:1,'%');
End;
Write(' . Press ENTER. ');
ReadLn;
End; { CodeTest }
Procedure Settings;
{ ***********************************************************
Get Frequency in Hz.
Get Pause time. Pause time is 1000th of a second.
***********************************************************}
Begin
ClrScr;
Repeat
GoToXY(20,12); Write('Frequency in HZ ',HZ,': ');
ReadLn(Hz);
Until Hz > 200;
Repeat
GoToXY(20,14); Write('Delay time between chars ',Pause,': ');
ReadLn(Pause);
Until Pause > 10;
End; { Settings }
Function Menu:Integer;
{ ***********************************************************
Display menu and return with number selection.
***********************************************************}
Var
Choice : Integer;
Ch : Char;
Begin
Repeat
TextColor(White);
TextBackground(Black);
ClrScr;
GoToXY(20,7);
Write('MORSE CODE PROGRAM. RELEASED TO PUBLIC DOMAIN BY');
GoToXY(20,8);
Write('JERRY ADKINS KB8YA/6 COMPUSERVE ID: 70455,112');
GoToXY(30,10); Write('1 - CODE PRACTICE');
GoToXY(30,12); Write('2 - READ TEXT FILE');
GoToXY(30,14); Write('3 - RANDOM CODE');
GoToXY(30,16); Write('4 - SET SPEED AND TONE');
GoToXY(30,18); Write('5 - CODE TEST');
GoToXY(30,20); Write('Press ESC to exit.');
Ch := ReadKey;
If Ch = #0 Then
Ch := ReadKey;
Case Ch Of
Esc :
Begin
Menu := 0;
Exit;
End;
'1'..'5' :
Begin
Menu := Ord(Ch)-48;
Exit;
End;
End; { Case }
Until DONE;
End; { Menu }
Begin { Main Program }
{ ***********************************************************
Main part of program. Control branching and exit to DOS.
***********************************************************}
Repeat
Cmd := Menu;
Case Cmd Of
1: MorseKey;
2: MorseRead;
3: RandomCode;
4: Settings;
5: CodeTest;
End; { Case }
Until Cmd = 0;
End. { Morse }